/**
 * 更新VNodeDatas中的attrs相关
 */

import {checkIsVnode} from './vnode'

export function updateAttrs(oldVnode,vnode){

    if(!checkIsVnode([oldVnode,vnode])) return

     let elm=vnode.elm,
     oldAttrs=oldVnode.data&&oldVnode.data.attrs||{},
     attrs=vnode.data&&vnode.data.attrs||{}

     if(!oldAttrs&&!attrs) return
     if(oldAttrs===attrs) return

     /**update modified attrs and add new attrs */
     for(let key in attrs){
        const cur=attrs[key],
        old=oldAttrs[key]
        
        if(old!==cur){
            if(cur===true){
                elm.setAttribute(key,'')
            }else if(cur===false){
                elm.removeAttribute(key)
            }else{
                elm.setAttribute(key,cur)
            }
        }
        
     }

     /**remove previous and no need attrs */
     for(let key in oldAttrs){
         if(!(key in attrs)) elm.removeAttribute(key)
     }
     
 }